package Server_Netty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

public class GetDataFromServer extends Thread{
    public static final Logger LOGGER = LoggerFactory.getLogger(GetDataFromServer.class);

    int Data_RoleId;
    public GetDataFromServer(int roleid) {
        Data_RoleId = roleid;
    }

    public void run() {
        DO_SAVE();
    }

    public void DO_SAVE() {
        int cycle = 1000;
        //Todo:这里需要更换为实际的地址
        while (true) {
            try {
                FileOutputStream fos = new FileOutputStream("D:/Java_workspace/netty_model/" + Data_RoleId + ".yuv", true);

//                if (cycle == 0) {
//                    cycle = 1000;
//                    LOGGER.info("Do save: " + Data_RoleId);
//                }
//                cycle--;

                if (!(Main_Send_Receive_Manager.RoleID_ReceiveQueue.isEmpty())) {
                    LinkedBlockingQueue<DataCallBackClass> SendDataQueue=Main_Send_Receive_Manager.RoleID_ReceiveQueue.get(Data_RoleId);
                    if (SendDataQueue != null) {
                        for (int i = 0; i < 100000; i++) {
                            if (!(SendDataQueue.isEmpty())) {
                                DataCallBackClass rece_Data = null;

                                rece_Data = SendDataQueue.poll();
                                if (null == rece_Data) {
                                    continue;
                                }

                                //Todo：这里采用的是将接收到的数据写入文件，根据实际需求更改
                                int DataLen = rece_Data.getDataLen();
                                byte Rece_Data[] = rece_Data.getData_content();

                                fos.write(Rece_Data, 0, DataLen);
                            }
                        }
                    }
                }
                fos.close();
                Thread.sleep(1);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
